package com.example.yifei.ui.page.widgets

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.yifei.ui.theme.buttonCorner
import com.example.yifei.ui.theme.buttonHeight
import com.example.yifei.ui.theme.white1
import com.google.accompanist.placeholder.material.placeholder
import org.jetbrains.annotations.NotNull


@Composable
fun AppButton(
    text: String,
    modifier: Modifier = Modifier,
    bgColor: Color = Color(0xFFF7F7F7),
    textColor: Color = Color(0xFFD5D5D5),
    onClick: () -> Unit
) {
    Box(
        modifier = modifier
            .fillMaxWidth()
            .height(buttonHeight)
            .background(color = bgColor, shape = RoundedCornerShape(buttonCorner))
            .clickable {
                onClick()
            }
    ) {
        TextContent(text = text, color = textColor, modifier = Modifier.align(Alignment.Center))
    }
}


@Composable
fun PrimaryButton(
    text: String,
    modifier: Modifier = Modifier,
    onClick: () -> Unit
) {
    AppButton(
        text = text,
        modifier = modifier,
        textColor = Color(0xFFD5D5D5),
        onClick = onClick,
        bgColor = Color(0xFF336AF8)
    )
}

@Composable
fun SecondlyButton(
    text: String,
    modifier: Modifier = Modifier,
    onClick: () -> Unit
) {
    AppButton(
        text = text,
        modifier = modifier,
        textColor = Color(0xFF888888),
        onClick = onClick
    )
}


@ExperimentalFoundationApi
@Composable
fun LabelTextButton(
    @NotNull text: String,
    modifier: Modifier = Modifier,
    isSelect: Boolean = true,
    specTextColor: Color? = null,
    cornerValue: Dp = 25.dp / 2,
    isLoading: Boolean = false,
    onClick: (() -> Unit)? = null,
    onLongClick: (() -> Unit)? = null
) {
    Text(
        text = text,
        modifier = modifier
            .height(25.dp)
            .clip(shape = RoundedCornerShape(cornerValue))
            .background(
                color = if (isSelect && !isLoading) Color(0xFF1E1E1E) else Color(0xFFF7F7F7),
            )
            .padding(
                horizontal = 10.dp,
                vertical = 3.dp
            )
            .combinedClickable(
                enabled = !isLoading,
                onClick = { onClick?.invoke() },
                onLongClick = { onLongClick?.invoke() }
            )
            .placeholder(
                visible = isLoading,
                color = Color(0xFF888888)
            ),
        fontSize = 13.sp,
        textAlign = TextAlign.Center,
        color = specTextColor ?: if (isSelect) white1 else Color(0xFF888888),
        overflow = TextOverflow.Ellipsis,
        maxLines = 1,
    )
}


/**
 * 渐变色按钮
 */
@ExperimentalMaterialApi
@Composable
fun MyButton(
    text: String,
    textColor: Color = Color.White,
    fontSize: TextUnit = 16.sp,
    contentPadding: PaddingValues = PaddingValues(
        start = 16.dp,
        top = 6.dp,
        end = 16.dp,
        bottom = 6.dp
    ),
    bgColors: List<Color> = listOf(
        Color(0xff7AB5FF),
        Color(0xff336AF8),
    ),
    onClick: () -> Unit
) {
    Row(
        Modifier
            .background(
                brush = Brush.horizontalGradient(
                    colors = bgColors,
                ), shape = RoundedCornerShape(4.dp)
            )
            .clickable {
                onClick()
            },
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) @Composable {
        Text(
            text = text,
            color = textColor,
            fontSize = fontSize,
            modifier = Modifier
                .padding(contentPadding)
        )
    }
}